package bambook.views;


import java.util.ArrayList;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.*;
import org.eclipse.swt.SWT;
import com.sdo.bambooksdk.BambookCore;
import com.sdo.bambooksdk.Const;
import com.sdo.bambooksdk.structure.BambookBookInfo;
import com.sun.jna.ptr.IntByReference;

/**
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class BambookView extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "bambook.views.BambookView";
	public static int currentProgress = 0;
	private TableViewer viewer;

	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}
		public void dispose() {
		}
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public Object[] getElements(Object parent) {
			ArrayList BookList=new ArrayList(); 
	        int hConn = 0;
	        int timeOut = 0;
	        IntByReference pConn = new IntByReference();
	        int expResult = 0;
	       // int result = BambookCore.INSTANCE.BambookConnect(Const.DEFAULT_BAMBOOK_IP, timeOut, pConn);
	        int result = BambookCore.INSTANCE.BambookConnect("127.0.0.1", timeOut, pConn);
	        if (expResult == result) {
	        	hConn =  pConn.getValue();
	        }
	        BambookBookInfo pInfo = new BambookBookInfo();
	        pInfo.cbSize = pInfo.size();
	        result = BambookCore.INSTANCE.BambookGetFirstPrivBookInfo(hConn, pInfo);
	        while (Const.BR_SUCC == result) {
	        	String strResult = new String(pInfo.bookName);
	        	BookList.add(strResult);
	            pInfo=new BambookBookInfo();
	            pInfo.cbSize=pInfo.size();
	            result = BambookCore.INSTANCE.BambookGetNextPrivBookInfo(hConn, pInfo);
	            currentProgress = 0;
	        }
	        BambookCore.INSTANCE.BambookDisconnect(hConn);
	        
	        return BookList.toArray();
		}
	}

	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}
		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}
		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().
					getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
		}
	}
	class NameSorter extends ViewerSorter {
	}

	/**
	 * The constructor.
	 */
	public BambookView() {
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setSorter(new NameSorter());
		viewer.setInput(getViewSite());
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
}